<?php

namespace App\Console\Commands;

use App\Events\RechargeBillRecord;
use App\Events\RemitBillRecord;
use App\Models\RechargeOrder;
use App\Models\RemitOrder;
use Illuminate\Console\Command;

class WriteHisToBillNo extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'bills:write-history';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        ini_set('memory_limit', '256M');
        $rechargeOrder = RechargeOrder::where('order_status', 1)->where('is_settle', 1)->get();
        foreach ($rechargeOrder->chunk(200) as $orders) {
            foreach ($orders as $order) {
                event(new RechargeBillRecord($order));
            }
        }

        $remitOrder = RemitOrder::where('status', 3)->get();
        foreach ($remitOrder->chunk(200) as $remits) {
            foreach ($remits as $remit) {
                event(new RemitBillRecord($remit));
            }
        }
    }
}
